import { Dictionary } from '@/interface/Basic'
export const formatTime = (dateString: string | number, mode: string) => {
  if (dateString === '' || dateString === 0 || !dateString) return ''
  const date = new Date(dateString)
  const dateObj: Dictionary<number | string> = {}
  dateObj.Y = date.getFullYear()
  dateObj.M = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1
  dateObj.D = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
  dateObj.h = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()
  dateObj.m = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()
  dateObj.s = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds()
  // mode: YYYY-MM-DD-hh-mm-ss
  const dateArr: Array<string | number> = []
  const timeArr: Array<string | number> = []
  mode.split('-').forEach(item => {
    if (['Y', 'M', 'D'].includes(item[0])) dateArr.push(dateObj[item[0]])
    if (['h', 'm', 's'].includes(item[0])) timeArr.push(dateObj[item[0]])
  })
  return `${dateArr.join('-')} ${timeArr.join(':')}`.trim()
}

export const deepClone = function <T>(target: T): T {
  // 目标值为 非引用类型，直接返回目标值
  if (typeof target !== 'object') {
    return target
  }
  let temp: any
  if (Array.isArray(target)) {
    temp = []
    target.forEach(item => {
      temp.push(deepClone(item))
    })
  } else {
    temp = {}
    for (const item in target) {
      if (target[item] instanceof Date) {
        const time = new Date(String(target[item]))
        temp[item] = time
      } else {
        temp[item] = deepClone(target[item])
      }
    }
  }
  return temp
}
